package com.lkm.app_ui.m.Imple.vip;

import com.lkm.app_ui.help.AppUIHelp;
import com.lkm.app_ui.help.HandleDialog;
import com.lkm.app_ui.help.TaskHelp;
import com.lkm.app_ui.m.fun.DelegatePayTask;
import com.lkm.app_ui.m.fun.OnNfcDataEvent;
import com.lkm.app_ui.m.vip.OnVipEvent;
import com.lkm.app_ui.m.vip.VipInfo;
import com.lkm.app_ui.m.vip.VipModule;
import com.lkm.app_ui.ui.sale.SaleContract;
import com.lkm.comlib.ui.BaseFragmentActivity;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.BackCallAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;



public class VipModuleImpl implements VipModule {

    private VipInfo curVipInfo;

    public VipModuleImpl() {

        regOnVipCard();
        regOnVipInfo();

        new LooperReadNfcData().start();
        OnVipEvent.sendOK(curVipInfo);
    }




    protected void loadVipInfo(String vipsign) {
        TaskHelp.cancelById(LoadVipTask.getTaskId());
        LoadVipTask loadVipTask = new LoadVipTask(vipsign);
        loadVipTask.action();
        loadVipTask.addBackCall(new BackCallAdapter<Object, ResponEntity<VipTo>>() {

            @Override
            protected void onEnd(Task<?, Object, ResponEntity<VipTo>> task, ResponEntity<VipTo> re, boolean isCancel) {
                super.onEnd(task, re, isCancel);

//                re.getData().name="lkm_";
//                re.getData().vipNo=vipsign;
//                re.getData().amount="100";

                if (re != null && re.isSuccess() && re.getData() != null) {
                    VipTo data = re.getData();
                    curVipInfo = new VipInfo();
                    curVipInfo.setAmount(data.amount);
                    curVipInfo.setName(data.name);
                    curVipInfo.setOid(data.oid);
                    curVipInfo.setPhone(data.phone);
                    curVipInfo.setTotal(data.total);
                    curVipInfo.setUnit(data.unit);
                    curVipInfo.setVipNo(data.vipNo);
                    OnVipEvent.sendOK(curVipInfo);
                } else {
                    OnVipEvent.sendFail(re == null ? "" : re.getMsg());
                }
            }
        });
        if (loadVipTask.execTask() != Task.serialNum_not) {
            OnVipEvent.sending();
        }
    }

    @Override
    public VipInfo curVipInfo() {
        return curVipInfo;
    }

    @Override
    public void exitVip() {
        curVipInfo = null;
        OnVipEvent.sendOK(null);
    }


    private void regOnVipCard() {
        LinkBus.registerReceiver(new ObserverAutoNext<OnNfcDataEvent>() {
            @Override
            public void onReceiver(OnNfcDataEvent data) {
                if (!StringHelp.isEmpty(data.getNfcData()))
                    loadVipInfo(data.getNfcData());
            }

            @Override
            public Class getBusEventClass() {
                return OnNfcDataEvent.class;
            }
        });

    }

    private void regOnVipInfo() {
        LinkBus.registerReceiver(new ObserverAutoNext<OnVipEvent>() {
            HandleDialog handleDialog;
            @Override
            public void onReceiver(OnVipEvent data) {

                if (handleDialog != null) {
                    handleDialog.dismiss();
                }
                if (data.isIntercepted()) return;
                if (data.isIng()) {
                    handleDialog = new HandleDialog();
                    handleDialog.show(BaseFragmentActivity.getLastActivity(), "加载会员信息中", OnSteelyardKeyEvent.KEYCODE_9, "关闭（按9）", new Runnable() {
                        @Override
                        public void run() {
                            handleDialog = null;
                            TaskHelp.cancelById(LoadVipTask.getTaskId());
                        }
                    }, false);
                } else if (data.isFail()) {
                    AppUIHelp.tryShowTipsSHORT(data.getError());
                } else if (data.getVipInfo() != null) {



//                    SaleContract.subtotalPresenter.tryFullPay(DelegatePayTask.PayType_Delegate);
                }
            }

            @Override
            public Class getBusEventClass() {
                return OnVipEvent.class;
            }
        });

    }





//        NFC_Module nfc_module = AppConst.getModuleManager().peekModule(NFC_Module.class);












}
